package com.yunwaikeji.gy_tool_library.mvvm.view.fragment

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.weikaiyun.fragmentation.SupportFragment
import com.yunwaikeji.gy_tool_library.info.GYSmartRefreshInfo
import com.yunwaikeji.gy_tool_library.mvvm.view.admin.GYViewAdmin
import com.yunwaikeji.gy_tool_library.utils.GYStatusBarUtils

abstract class GYFragment : SupportFragment(), GYFragmentInterface, GYSmartRefreshInfo {
    val mViewAdmin = GYViewAdmin.newInstance()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mViewAdmin.create(inflater, getLayoutId())
        return mViewAdmin.view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        context?.let { context ->
            initDataObserver(context)
            initView(context, view, savedInstanceState)
            Handler(Looper.getMainLooper()).postDelayed({
                initViewDelayed(context, view, savedInstanceState)
                initData(true)
            }, mViewAdmin.initViewDelayed)
        }
        mViewAdmin.loadingLayout.setOnErrorTextClickListener {
            showLayoutLoading()
            initData(true)
        }
    }

    override fun onVisible() {
        super.onVisible()
        GYStatusBarUtils.setTextColorDark(activity, mViewAdmin.isStatusContentDark)
    }

    open fun setStatusBarContentIsDark(statusContentDark: Boolean) {
        mViewAdmin.isStatusContentDark = statusContentDark
        onVisible()
    }

    override fun showTopBar() {
        showTopBar(true)
    }

    override fun showTopBar(show: Boolean) {
        if (show) mViewAdmin.topBar.visibility = View.VISIBLE else mViewAdmin.topBar.visibility =
            View.GONE
    }

    override fun showLoadingDialog() {
        mViewAdmin.loadingDialog.show()
    }

    override fun showLoadingDialog(text: String?) {
        mViewAdmin.loadingDialog.setTipText(text)
        mViewAdmin.loadingDialog.show()
    }

    override fun dismissLoadingDialog() {
        mViewAdmin.loadingDialog.dismiss()
    }

    override fun showLayoutContent() {
        mViewAdmin.loadingLayout.showContent()
    }

    override fun showLayoutLoading() {
        mViewAdmin.loadingLayout.showLoading()
    }

    override fun showLayoutLoading(text: String?) {
        mViewAdmin.loadingLayout.showLoading(text)
    }

    override fun showLayoutError() {
        mViewAdmin.showError("加载失败")
    }

    override fun showLayoutError(text: String?) {
        mViewAdmin.loadingLayout.showError(text)
    }
}